package com.umakr.ax.common.service;

import com.baomidou.mybatisplus.plugins.Page;
import com.umakr.ax.common.mapper.FeedbackMapper;
import com.umakr.ax.common.model.Feedback;
import com.umakr.ax.core.base.BaseService;
import com.umakr.ax.utils.FormatPhotoUrlUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by umakr on 2018/5/17.
 * 用户反馈
 * @author zj
 */

@Service
public class FeedbackService extends BaseService<FeedbackMapper, Feedback> {

    @Value("${oss.img.path}")
    public String aliUrl;

    public Object saveFeedbackInfo(Feedback feedback) {
        int errorCode = 0 ;
        String msg ;
        Date date = new Date();
        feedback.setTime(date);
        baseMapper.saveFeedbackInfo(feedback);
        msg = "反馈成功";
        errorCode = 1;
        return  resultMap(errorCode,msg);
    }

    public Object listFeedback(Integer type, String startTime, String endTime,String systemName,
                               Integer confirmStatus,Integer solveStatus) {
        Date date1 = null;
        Date date2 = null;
        if("".equals(type)) {
            type = null;
        }
        if("".equals(confirmStatus)) {
            confirmStatus = null;
        }
        if("".equals(solveStatus)) {
            solveStatus = null;
        }


        if(!"".equals(startTime)) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
            try {
                date1 = sdf.parse(startTime);
                date2 = sdf.parse(endTime);
            } catch (Exception e) {
                logger.debug(e);
            }
        }

        Page<Feedback> page = baseMapper.listFeedback(type,date1,date2,systemName,confirmStatus,solveStatus);

        for(Feedback feedback : page.getRecords()){
            //设置图片路径
            feedback.setScreenshoot(FormatPhotoUrlUtil.formatStringArray(aliUrl,feedback.getScreenshoot()));
        }

        return page;
    }

    public Object updateFeedback(Long id,Integer confirmStatus, Integer solveStatus,Integer delStatus) {
        int errorCode = 0 ;
        String msg;
        if("".equals(confirmStatus)) {
            confirmStatus = null;
        }
        if("".equals(solveStatus)) {
            solveStatus = null;
        }
        if("".equals(delStatus)) {
            delStatus = null;
        }
        baseMapper.updateFeedback(id,confirmStatus,solveStatus,delStatus);
        msg = "操作成功";
        errorCode = 1;
        return  resultMap(errorCode,msg);
    }

    public Object delectFeedback(Long id) {
        int errorCode = 0 ;
        String msg;
        baseMapper.deleteById(id);
        msg = "删除成功";
        errorCode = 1;
        return  resultMap(errorCode,msg);
    }
}

